package com.dlxj.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dlxj.dao.BaseDAO;
import com.dlxj.entity.Model;
import com.dlxj.entity.Purview;
import com.dlxj.service.ModelService;
import com.dlxj.service.PurviewService;
import com.dlxj.util.ConstClass;

public class PurviewServiceImpl implements PurviewService {

	BaseDAO baseDAO;
	ModelService modelService;

	@Override
	public List<Model> findModelByPid(int pid) {
		Purview purview = (Purview) baseDAO.findById(Purview.class, pid);
		String midStr = purview.getMid();
		List<Model> models = new ArrayList<Model>();
		List<Model> result = new ArrayList<Model>();
		if (midStr != null) {
			models = (List<Model>) baseDAO
					.findByHql("from Model  order by pid,id");

			for (Model model : models) {
				for (String str : midStr.split(",")) {
					if (model.getId().intValue() == Integer.valueOf(str)
							.intValue()) {
						result.add(model);
						break;
					}
				}
			}

		}
		return result;
	}

	@Override
	public int addPurview(List<Model> models) {
		String mid = "";
		List<Integer> pids = new ArrayList<Integer>();
		for (Model model : models) {
			Model m = (Model) baseDAO.findById(Model.class, model.getId());
			boolean flag = false;
			for (Integer id : pids) {
				if (id == m.getPid()) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				mid += m.getId() + ",";
			}
		}
		if (mid.length() > 0) {
			mid = mid.substring(0, mid.length() - 1);
			Purview purview = new Purview(mid);
			return (Integer) baseDAO.save(purview);
		}
		return ConstClass.ADD_ERROR;
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public ModelService getModelService() {
		return modelService;
	}

	public void setModelService(ModelService modelService) {
		this.modelService = modelService;
	}

	@Override
	public List<Model> findModels() {

		return (List<Model>) baseDAO.findByHql("from Model where pid !=id");
	}

	@Override
	public List<Model> findModelByPid(int pid, boolean clearParent) {
		List<Model> models = this.findModelByPid(pid);
		List<Model> result = new ArrayList<Model>();
		for (Model model : models) {
			if (model.getId() != model.getPid()) {
				result.add(model);
			}
		}
		return result;
	}

	@Override
	public int updatePurview(int pid, String mid) {
		String modelIds = "";
		List<Model> models = new ArrayList<Model>();
		for (String id : mid.split(",")) {
			models.add((Model) baseDAO.findById(Model.class,
					Integer.parseInt(id)));
		}
		List<Integer> pids = new ArrayList<Integer>();
		for (Model model : models) {
			boolean flag = false;
			for (Integer id : pids) {
				if (id.intValue() == model.getPid().intValue()) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				modelIds += model.getPid() + ",";
				pids.add(model.getPid());
			}
			modelIds += model.getId() + ",";
		}
		if (modelIds.length() > 0) {
			modelIds = modelIds.substring(0, modelIds.length() - 1);
			Purview purview = (Purview) baseDAO.findById(Purview.class, pid);
			purview.setMid(modelIds);
			baseDAO.update(purview);
			return ConstClass.UPDATE_SUCCESS;
		}
		return ConstClass.UPDATE_FAILURE;
	}

	@Override
	public Purview findPurviewById(int pid) {

		return (Purview) baseDAO.findById(Purview.class, pid);
	}

}
